/***********************************************
 * Title:       SubjectAction.java
 * Description: SubjectAction.java
 * Author:      Niyuzhe
 * Create Date: 2010-1-25
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.education;



import org.apache.log4j.Logger;
import org.infohazard.maverick.flow.ControllerContext;

import com.younker.waf.maverick.DispatchCtl;

import edu.tju.education.model.ISubjectService;
import edu.tju.education.model.Subject;
import edu.tju.education.model.SubjectService;

/**
 * @author Niyuzhe
 *
 */
public class SubjectManageAction extends DispatchCtl
{
        
        private Logger log = Logger.getLogger(this.getClass());
        
        private ISubjectService subjectService = new SubjectService();
        
        //return value
        private static String RC_NOSUBJECT = "4";
        
        //the parameter from web page
        private long subjectId;
        private String subjectName;
        private long teacherId;
        private long teacherSubjectId;
        private String subjectCate;
        
        //view name
        private static String SUBJECT_INFO = "subjectInfo";


        /* (non-Javadoc)
         * @see org.infohazard.maverick.ctl.FormBeanUser#makeFormBean(org.infohazard.maverick.flow.ControllerContext)
         */
        @Override
        protected Object makeFormBean(ControllerContext arg0)
        {
                return new SubjectManageAction();
        }
        
        public String displaySubject( Object beans, ControllerContext ctx )
        {
                log.debug("in displaySubject");
                Subject subject = subjectService.getSubjectById(((SubjectManageAction)beans).getSubjectId());
                
                if( subject == null )
                {
                        ctx.getRequest().setAttribute(RET_CODE, RC_NOSUBJECT);
                }else
                {
                        ctx.setModel(subject);                        
                }
                
                return SUBJECT_INFO;
                
        }
        
        public String saveOrUpdate( Object beans, ControllerContext ctx )
        {
                SubjectManageAction bean = (SubjectManageAction)beans;
                log.debug(bean);
                long id = bean.getSubjectId();
                Subject subject;
                if( id == 0 )
                {
                        subject = new Subject();
                        subject.setCate(bean.getSubjectCate());
                        subject.setName(bean.getSubjectName());
                        if( subjectService.insertSubject(subject) == -1 )
                        {
                                ctx.getRequest().setAttribute(RET_CODE, RC_FAILURE);                                
                        }else
                        {
                                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                        }                                
                }else
                {
                        subject = subjectService.getSubjectById(id);
                        subject.setName(bean.getSubjectName());
                        subject.setCate(bean.getSubjectCate());
                        if( subjectService.updateSubject(subject) == 0 )
                        {
                                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                        }else
                        {
                                ctx.getRequest().setAttribute(RET_CODE, RC_FAILURE);
                        }
                }
                
                ctx.setModel(subject);
                
                return SUBJECT_INFO;
        }
        
        public String setTeacher( Object beans, ControllerContext ctx )
        {
                SubjectManageAction bean = (SubjectManageAction) beans;
                
                subjectService.setTeacherSubject(bean.getSubjectId(), bean.getTeacherId());        
                
                ctx.setModel(subjectService.getSubjectById(bean.getSubjectId()));
                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                
                return SUBJECT_INFO;
        }
        
        public String unsetTeacher(Object beans, ControllerContext ctx)
        {
                SubjectManageAction bean = (SubjectManageAction) beans;

                subjectService.unsetTeacherSubject(bean.getTeacherSubjectId());

                ctx.setModel(subjectService.getSubjectById(bean.getSubjectId()));
                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);

                return SUBJECT_INFO;
        }


        /**
         * @return the subjectId
         */
        public long getSubjectId()
        {
                return subjectId;
        }

        /**
         * @param subjectId the subjectId to set
         */
        public void setSubjectId(long subjectId)
        {
                this.subjectId = subjectId;
        }

        /**
         * @param subjectName the subjectName to set
         */
        public void setSubjectName(String subjectName)
        {
                this.subjectName = subjectName;
        }

        /**
         * @return the subjectName
         */
        public String getSubjectName()
        {
                return subjectName;
        }

        /**
         * @return the teacherId
         */
        public long getTeacherId()
        {
                return teacherId;
        }

        /**
         * @param teacherId the teacherId to set
         */
        public void setTeacherId(long teacherId)
        {
                this.teacherId = teacherId;
        }

        /**
         * @return the teacherSubjectId
         */
        public long getTeacherSubjectId()
        {
                return teacherSubjectId;
        }

        /**
         * @param teacherSubjectId the teacherSubjectId to set
         */
        public void setTeacherSubjectId(long teacherSubjectId)
        {
                this.teacherSubjectId = teacherSubjectId;
        }

        
        /**
         * @return the subjectCate
         */
        public String getSubjectCate()
        {
                return subjectCate;
        }

        /**
         * @param subjectCate the subjectCate to set
         */
        public void setSubjectCate(String subjectCate)
        {
                this.subjectCate = subjectCate;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString()
        {
                return "SubjectManageAction [subjectCate=" + subjectCate + ", subjectId="
                                + subjectId + ", subjectName=" + subjectName + ", teacherId="
                                + teacherId + ", teacherSubjectId=" + teacherSubjectId + "]";
        }
        
        

}
